# Copyright (C) 2014 MediaSift Ltd.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

#
# Process ragel files
#
IF (RAGEL_FOUND)
	FILE (GLOB_RECURSE served_RL ${CMAKE_CURRENT_SOURCE_DIR}/*.rl)

	FOREACH (R_FILE ${served_RL})
		GET_FILENAME_COMPONENT (R_NAME ${R_FILE} NAME_WE)
		STRING (REPLACE ".rl" ".cpp" R_OUTFILE "${R_FILE}")
		RAGEL_TARGET (${R_NAME} ${R_FILE} ${R_OUTFILE})
		LIST (APPEND served_RLSOURCES ${RAGEL_${R_NAME}_OUTPUTS})
	ENDFOREACH (R_FILE ${served_RL})
ENDIF (RAGEL_FOUND)

#
# Locate project sources
#
FILE (GLOB_RECURSE served_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp)
FILE (GLOB_RECURSE served_HDRS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp)

#
# Locate test sources and remove from project sources
#
FILE (GLOB_RECURSE test_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.test.cpp)
LIST (REMOVE_ITEM served_SRCS ${test_SRCS})

#
# Configure common project settings
#
SET (served_LIBS ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
SET (served_BIN ${PROJECT_NAME})

IF (NOT DEFINED SERVED_BUILD_SHARED)
	IF (CMAKE_CROSSCOMPILING)
		SET(SERVED_BUILD_SHARED 0)
	ELSE (CMAKE_CROSSCOMPILING)
		SET(SERVED_BUILD_SHARED 1)
	ENDIF (CMAKE_CROSSCOMPILING)
ENDIF (NOT DEFINED SERVED_BUILD_SHARED)

IF (NOT DEFINED SERVED_BUILD_STATIC)
	SET(SERVED_BUILD_STATIC 1)
ENDIF (NOT DEFINED SERVED_BUILD_STATIC)

IF (SERVED_BUILD_SHARED)
	SET(SERVED_TARGET_STATIC_SUFFIX "-static")
ELSE (SERVED_BUILD_SHARED)
	SET(SERVED_TARGET_STATIC_SUFFIX "")
ENDIF (SERVED_BUILD_SHARED)

#
# Static lib build rules
#
IF (SERVED_BUILD_STATIC)
	ADD_LIBRARY (${PROJECT_NAME}${SERVED_TARGET_STATIC_SUFFIX} STATIC ${served_SRCS} ${served_RLSOURCES})
	TARGET_LINK_LIBRARIES (${PROJECT_NAME}${SERVED_TARGET_STATIC_SUFFIX} ${served_LIBS})
	SET_TARGET_PROPERTIES (${PROJECT_NAME}${SERVED_TARGET_STATIC_SUFFIX} PROPERTIES VERSION "${APPLICATION_VERSION_MAJOR}.${APPLICATION_VERSION_MINOR}" OUTPUT_NAME ${served_BIN} CLEAN_DIRECT_OUTPUT 1)
	INSTALL (TARGETS ${PROJECT_NAME}${SERVED_TARGET_STATIC_SUFFIX} DESTINATION lib)
ENDIF (SERVED_BUILD_STATIC)

#
# Shared object build rules
#
IF (SERVED_BUILD_SHARED)
	ADD_LIBRARY (${PROJECT_NAME} SHARED ${served_SRCS} ${served_RLSOURCES})
	TARGET_LINK_LIBRARIES (${PROJECT_NAME} ${served_LIBS})
	SET_TARGET_PROPERTIES (${PROJECT_NAME} PROPERTIES VERSION "${APPLICATION_VERSION_MAJOR}.${APPLICATION_VERSION_MINOR}" OUTPUT_NAME ${served_BIN} CLEAN_DIRECT_OUTPUT 1)
	INSTALL (TARGETS ${PROJECT_NAME} DESTINATION lib)
ENDIF (SERVED_BUILD_SHARED)

#
# Configure common test settings
#
SET (test_LIBS ${Boost_LIBRARIES} ${PROJECT_NAME})
SET (test_HDRS "../test/catch.cpp")

#
# Test build rules
#
IF (SERVED_BUILD_TESTS)
	IF (NOT CMAKE_CROSSCOMPILING)
		LINK_DIRECTORIES (${MAINFOLDER}/lib)
		FOREACH (T_FILE ${test_SRCS})
			GET_FILENAME_COMPONENT (T_NAME ${T_FILE} NAME_WE)
			SET (T_NAME "${T_NAME}_test")
			ADD_EXECUTABLE (${T_NAME} ${T_FILE} ${test_HDRS})
			TARGET_LINK_LIBRARIES (${T_NAME} ${test_LIBS})
			ADD_TEST (${T_NAME} ${MAINFOLDER}/bin/${T_NAME})
		ENDFOREACH (T_FILE ${test_SRCS})
	ENDIF (NOT CMAKE_CROSSCOMPILING)
ENDIF (SERVED_BUILD_TESTS)

#
# Install target
#
FOREACH (H_FILE ${served_HDRS})
	SET (H_INSTALL_DIR "served")
	STRING (REGEX MATCH "(.*)[/\\]" DIR ${H_FILE})
	INSTALL (FILES ${H_FILE} DESTINATION include/${H_INSTALL_DIR}/${DIR})
ENDFOREACH (H_FILE ${served_HDRS})
